<?php
/**
 * Created by PhpStorm.
 * User: sky
 * Date: 2019/7/16
 * Time: 23:04
 */

namespace App\Repositories;

use App\Models\GoodsAttributes;
use App\Models\GoodsAttributesValue;
use Illuminate\Support\Facades\DB;

class GoodsAttributesRepositories
{
    public function model()
    {
        return GoodsAttributes::query();
    }

    public function find($id)
    {
        return $this->model()->with('hasManyValue')->where('id',$id)->first();
    }

    public function save($data)
    {
        DB::beginTransaction();
        try
        {
            $res =$this->model()->create($data);
            $value =[];
            if(isset($data['value']) && is_array($data['value']))
            {
                foreach ($data['value'] as $key => $val)
                {
                    $value[] = [
                        'attribute_id' =>$res->id,
                        'value'=>$val
                    ];
                }
                GoodsAttributesValue::insert($value);
                DB::commit();
            }
            return $res;

        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            throw new \Exception($exception->getMessage());
        }

    }
    public function update($id,$data)
    {
        DB::beginTransaction();
        try
        {
            $updateData=[
                'name'=>$data['name'],
                'sort'=>$data['sort'],
            ];
            $res = $this->model()->where('id',$id)->update($updateData);

            GoodsAttributesValue::where('attribute_id', $id)->delete();

            $value =[];
            if(isset($data['value']) && is_array($data['value']))
            {
                foreach ($data['value'] as $key => $val)
                {
                    $value[] = [
                        'attribute_id' =>$id,
                        'value'=>$val
                    ];
                }
                GoodsAttributesValue::insert($value);
                DB::commit();
            }

            return $res;

        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            throw new \Exception($exception->getMessage());
        }
    }

    public function search($data)
    {
        $model = $this->model()->with('hasManyValue');

        if(isset($data['name']))
        {
            $model = $model->where('name','like','%'.$data['name'].'%');
        }

        $data['limit'] = isset($data['limit'])?$data['limit']:999;

        //些处扩展更多查询条件
        return $model->orderBy('created_at','desc')->paginate($data['limit'])->toArray();
    }

    public function delete($ids)
    {
        DB::beginTransaction();
        try
        {
            $res =GoodsAttributes::destroy($ids);
            GoodsAttributesValue::whereIn('attribute_id', $ids)->delete();
            DB::commit();
            return $res;
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            throw new \Exception($exception->getMessage());
        }
    }

}